package pers.qianyu.month_202102.date_20210207.weekly_contest_227;

import org.junit.*;

import java.util.*;

/**
 * [第 227 场周赛 第一题]
 * 5672. 检查数组是否经排序和轮转得到
 * https://leetcode-cn.com/problems/check-if-array-is-sorted-and-rotated/
 *
 * @author mizzle rain
 * @date 2021年2月7日10:45:15
 */
public class Check {
    public boolean check(int[] nums) {
        int len = nums.length;
        int[] arrCopy = Arrays.copyOf(nums, len);
        Arrays.sort(arrCopy);
        for (int i = 0; i < len; i++) {
            int[] res = solve(arrCopy, i);
            if (Arrays.equals(res, nums)) {
                return true;
            }
        }
        return false;
    }

    private int[] solve(int[] A, int cur) {
        int[] B = new int[A.length];
        for (int i = 0; i < A.length; i++) {
            B[(i + cur) % A.length] = A[i];
        }
        return B;
    }

    @Test
    public void test1() {
        boolean result = new Check().check(new int[]{3, 4, 5, 1, 2});
        System.out.println("result = " + result);
    }

    @Test
    public void test2() {
        boolean result = new Check().check(new int[]{2, 1, 3, 4});
        System.out.println("result = " + result);
    }

    @Test
    public void test3() {
        boolean result = new Check().check(new int[]{1, 2, 3});
        System.out.println("result = " + result);
    }

    @Test
    public void test4() {
        boolean result = new Check().check(new int[]{1, 1, 1});
        System.out.println("result = " + result);
    }

    @Test
    public void test5() {
        boolean result = new Check().check(new int[]{2, 1});
        System.out.println("result = " + result);
    }
}